package src.greedy_algorithms;

import java.util.Arrays;

/**
 * @author starsea
 * @date 2024-08-22 12:17
 */

public class Test01 {
    public static void main(String[] args) {
        int[]arr={-8,3,-5,-3,-5,-2};
        int c=largestSumAfterKNegations(arr,6);
    }
    public static int largestSumAfterKNegations(int[] nums, int k) {
        Arrays.sort(nums);
        int sum=0;
        int i=0;
        for(;i<nums.length;i++)
        {
            if(nums[i]<0 && k>0)
            {
                nums[i]*=-1;
                k--;
            }
            else
            {
                break;
            }
        }
        Arrays.sort(nums);
        k=k%2;
        if(k==1)
        {
            nums[0]*=-1;
        }
        for(int j=0;j<nums.length;j++)
        {
            sum+=nums[j];
        }
        return sum;
    }
}
